home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / DELPHI / MSGSOCK.ZIP / MSGSOCK.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-08-04  |  5.2 KB  |  165 lines

  1. unit MsgSock;
  2.  
  3. {******************************************************************************
  4.  Nome:            MsgSock
  5.  Scopo:     MsgSock permette lo scambio di messaggi tra applicazioni diverse.
  6.  Generalitα:MsgSock permette di scambiare messaggi tra diverse applicazioni
  7.                          Windows che ospitino una sua istanza. Esso fornisce metodi per
  8.             inviare un messaggio in broadcast o ad una sua istanza di cui si
  9.             conosce l'handle. Inoltre dispone di un evento attivato alla
  10.             ricezione di un messaggio.
  11.  Autore:    Ing. M. Venturini
  12.  Revisore:
  13.  Data:      15 Jul. 96
  14.  Revisioni:
  15. ******************************************************************************}
  16.  
  17. interface
  18.  
  19. uses
  20.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls, Forms;
  21.  
  22. const
  23.     MsgSockReservedID = 1024;
  24.     MsgSockIDMin = wm_User + MsgSockReservedID;
  25.     MsgSockIDMax = MsgSockIDMin + MsgSockReservedID - 1;
  26.  
  27. type
  28.   TMsgReceived = procedure(                Sender: TObject;
  29.                                                    const    MsgID: Word;
  30.                                                    const    SourceHandle: Hwnd;
  31.                                                  const    DataPtr: Pointer) of object;
  32.   TMsgSock = class(TComponent)
  33.   private
  34.     WindowHandle: Hwnd;
  35.     FOnMsgReceived: TMsgReceived;
  36.     procedure WndProc(var Msg: TMessage);
  37.     public
  38.        procedure MsgBroadcast(const    MsgID: Word;
  39.                                                  const     DataPtr: Pointer);
  40.        procedure MsgSend(const    MsgID: Word;
  41.                                         const DestinationHandle: Hwnd;
  42.                                         const DataPtr: Pointer);
  43.     constructor Create(AOwner: TComponent); override;
  44.     destructor Destroy; override;
  45.   published
  46.       property Handle: Hwnd read WindowHandle;
  47.       property OnMsgReceived: TMsgReceived read FOnMsgReceived write FOnMsgReceived;
  48.   end;
  49.   EMsgSock = class(Exception);
  50.   EIllegalMsgID = class(EMsgSock);
  51.  
  52. procedure Register;
  53.  
  54. {*****************************************************************************}
  55.  
  56. implementation
  57.  
  58. const EIllegalMsgIDMsg = ' - Illegal message ID: ';
  59.  
  60. procedure Register;
  61. begin
  62.  
  63.   RegisterComponents('Additional', [TMsgSock]);
  64.  
  65. end;
  66.  
  67. {******************************************************************************
  68.  --    procedure WndProc [method]
  69.     Window procedure per il componente. Si occupa della ridirezione dei
  70.          messaggi non destinati al componente.
  71. ******************************************************************************}
  72.  
  73. procedure TMsgSock.WndProc(var Msg: TMessage);
  74. begin
  75.  
  76.   with Msg do
  77.     if (Msg >= MsgSockIDMin) and (Msg <= MsgSockIDMax) then begin
  78.       try
  79.                 if Assigned(FOnMsgReceived) then FOnMsgReceived(Self,Msg - MsgSockIDMin,Hwnd(WParam),Pointer(LParam));
  80.       except
  81.         Application.HandleException(Self);
  82.       end
  83.     end
  84.     else
  85.       Result := DefWindowProc(WindowHandle, Msg, WParam, LParam);
  86.  
  87. end;
  88.  
  89. {******************************************************************************
  90.  --    procedure MsgBroadcast [method]
  91.     Invia un messaggio in broadcast.
  92.  -- Parametri
  93.         MsgID:                    in Word
  94.                                     Identificatore che individua il tipo di messaggio.
  95.     DataPtr:                in Pointer
  96.                                     Puntatore ai dati che si vogliono trasmettere con il
  97.                     messaggio.
  98. ******************************************************************************}
  99.  
  100. procedure TMsgSock.MsgBroadcast(const    MsgID: Word;
  101.                                                                 const DataPtr: Pointer);
  102. begin
  103.  
  104.     if MsgId < MsgSockReservedID then
  105.       SendMessage(hwnd_Broadcast,MsgSockIDMin + MsgID,WindowHandle,Longint(DataPtr))
  106.   else
  107.       raise EIllegalMsgID.Create(TMsgSock.ClassName + EIllegalMsgIDMsg + IntToStr(MsgID));
  108.  
  109. end;
  110.  
  111. {******************************************************************************
  112.  --    procedure MsgSend [method]
  113.     Invia un messaggio ad un altro componente TMsgSock.
  114.  -- Parametri
  115.         MsgID:                    in Word
  116.                                     Identificatore che individua il tipo di messaggio.
  117.     DestinationHandle: in Hwnd
  118.                                     Handle del un componente TMsgSock a cui si vuole inviare
  119.                     il messaggio.
  120.     DataPtr:                in Pointer
  121.                                     Puntatore ai dati che si vogliono trasmettere con il
  122.                     messaggio.
  123. ******************************************************************************}
  124.  
  125. procedure TMsgSock.MsgSend(const MsgID: Word;
  126.                                                      const DestinationHandle: Hwnd;
  127.                                                  const DataPtr: Pointer);
  128. begin
  129.  
  130.     if MsgId < MsgSockReservedID then
  131.       SendMessage(DestinationHandle,MsgSockIDMin + MsgID,WindowHandle,Longint(DataPtr))
  132.   else
  133.       raise EIllegalMsgID.Create(TMsgSock.ClassName + EIllegalMsgIDMsg+ IntToStr(MsgID));
  134.  
  135. end;
  136.  
  137. {******************************************************************************
  138.  --    procedure Create [constructor]
  139.  Constructor per la classe.
  140. ******************************************************************************}
  141.  
  142. constructor TMsgSock.Create(AOwner: TComponent);
  143. begin
  144.  
  145.     inherited Create(AOwner);
  146.   WindowHandle := AllocateHWnd(WndProc);
  147.  
  148. end;
  149.  
  150. {******************************************************************************
  151.  --    procedure Destroy [destructor]
  152.  Destructor per la classe.
  153. ******************************************************************************}
  154.  
  155. destructor TMsgSock.Destroy;
  156. begin
  157.  
  158.   DeallocateHWnd(WindowHandle);
  159.   inherited Destroy;
  160.  
  161. end;
  162.  
  163.  
  164. end.
  165.